package com.kmproject.haifu.mvp.ui.bis.recharge;

import android.app.Activity;
import android.app.Application;

import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.ExchangeRateBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.MemberPayResultBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsRecharge;
import com.xgr.easypay.EasyPay;
import com.xgr.easypay.alipay.AliPay;
import com.xgr.easypay.alipay.AlipayInfoImpli;
import com.xgr.easypay.callback.IPayCallback;
import com.xgr.easypay.wxpay.WXPay;
import com.xgr.easypay.wxpay.WXPayInfoImpli;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

public class RechargeBasePresenter<M extends IModelRecharge, V extends IViewRecharge> extends BasePresenter<M, V> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    public RechargeBasePresenter(M model, V rootView) {
        super(model, rootView);
    }

    /**
     * 支付宝支付
     *
     * @param activity
     * @param datas
     */
    public void payForAli(Activity activity, String datas) {
        //实例化支付宝支付策略
        AliPay aliPay = new AliPay();
        //构造支付宝订单实体。一般都是由服务端直接返回。
        AlipayInfoImpli alipayInfoImpli = new AlipayInfoImpli();
        alipayInfoImpli.setOrderInfo(datas);
        //策略场景类调起支付方法开始支付，以及接收回调。
        EasyPay.pay(aliPay, activity, alipayInfoImpli, new IPayCallback() {
            @Override
            public void success() {
                mRootView.paySuccess();
            }

            @Override
            public void failed() {
                mRootView.showMessage("支付失败");
            }

            @Override
            public void cancel() {
                mRootView.showMessage("支付取消");
            }
        });
    }

    /**
     * 微信支付
     *
     * @param activity
     * @param memberPayResultBean
     */
    public void payForWX(Activity activity, MemberPayResultBean memberPayResultBean) {
//        PayReq request = new PayReq();
//        request.appId = memberPayResultBean.getAppid();
//        request.partnerId = memberPayResultBean.getPartnerid();
//        request.prepayId = memberPayResultBean.getPrepayid();
//        request.packageValue = memberPayResultBean.getPackageX();
//        request.nonceStr = memberPayResultBean.getNoncestr();
//        request.timeStamp = memberPayResultBean.getTimestamp();
//        request.sign = memberPayResultBean.getSign();
//        WxApiUtils.getWxApi(mAppManager.getCurrentActivity()).sendReq(request);


        //实例化微信支付策略
//        String wxAppId = "";
        WXPay wxPay = WXPay.getInstance(activity, memberPayResultBean.getAppid());
        //构造微信订单实体。一般都是由服务端直接返回。
        WXPayInfoImpli wxPayInfoImpli = new WXPayInfoImpli();
        wxPayInfoImpli.setTimestamp(memberPayResultBean.getTimestamp());
        wxPayInfoImpli.setSign(memberPayResultBean.getSign());
        wxPayInfoImpli.setPrepayId(memberPayResultBean.getPrepayid());
        wxPayInfoImpli.setPartnerid(memberPayResultBean.getPartnerid());
        wxPayInfoImpli.setAppid(memberPayResultBean.getAppid());
        wxPayInfoImpli.setNonceStr(memberPayResultBean.getNoncestr());
        wxPayInfoImpli.setPackageValue(memberPayResultBean.getPackageX());
        //策略场景类调起支付方法开始支付，以及接收回调。
        EasyPay.pay(wxPay, activity, wxPayInfoImpli, new IPayCallback() {
            @Override
            public void success() {
                mRootView.paySuccess();
            }

            @Override
            public void failed() {
                mRootView.showMessage("支付失败");
            }

            @Override
            public void cancel() {
                mRootView.showMessage("支付取消");
            }
        });
    }

    /**
     * 获取支付方式
     */
    public void getPayWay() {
        mModel.getPayWay(Api.PAY_METHOD).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getPayWaySuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }

                    }
                });
    }

    /**
     * 获取实时汇率
     */
    public void getExchangeRate(String amount) {
        mModel.getExchangeRate(amount).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<ExchangeRateBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<ExchangeRateBean> exchangeRateBeanResultBean) {
                        if (AppUtil.isSuccess(exchangeRateBeanResultBean)) {
                            mRootView.getExchangeRateSuccess(exchangeRateBeanResultBean.getData());
                        } else {
                            mRootView.showMessage(exchangeRateBeanResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取支付宝预支付信息
     */
    public void rechargeCallCharge4AliPay(ReqParamsRecharge reqParamsRecharge) {
        mModel.rechargeCallCharge4AliPay(AppUtil.entityToMap(reqParamsRecharge)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<String>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<String> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.rechargeCallCharge4AliPaySuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取微信预支付信息
     */
    public void rechargeCallCharge4Wx(ReqParamsRecharge reqParamsRecharge) {
        mModel.rechargeCallCharge4Wx(AppUtil.entityToMap(reqParamsRecharge)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<MemberPayResultBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<MemberPayResultBean> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.rechargeCallCharge4WxSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
}
